#####################################################################
# Do Personality Traits Predict Voter Attitudes When Politics Is    #
# Structured Around Conflict? Lessons From Israel                   #
#                                                                   #
# Rivlin-Angert, Yakter, and Sheffer                                #
# Public Opinion Quarterly                                          #
#                                                                   #
# Replication code - Figure E2                                      #
#####################################################################
# To load the data, users should place the relevant csv	file in     #
# in their R working directory (or type the full file paths as      #
# saved on their computer).                                         #
#####################################################################

## Required packages
library(foreign)
library(dplyr)
library(ggplot2)
library(patchwork)

## set-up data
# load data (data created from coefficients extracted from regression analyses in the main data code)
dat <- read.csv("Angert-Rivlin, Yakter, Sheffer POQ - figure E2 data.csv")
# re-order factor levels so they appear in desired order in the graph
dat$trait <- factor(dat$trait, level=c("agree","consc", "stabi","extra","openn"))
dat$religion <- factor(dat$religion,levels=c("all","sec","trad","relig","vrelig"))
dat$model <- factor(dat$model,levels=c("social","economic","conflict","lr"))

## plot (Figure E2)
# facet labels for subtitles
labeller_bf <- labeller(trait = c(agree = "Agreeableness", consc = "Conscientiousness",
                                  stabi = "Stability", extra = "Extraversion", openn = "Openness"),
                        model = c(social = "DV: Social", economic = "DV: Economic",
                                  conflict = "DV: Conflict", lr = "DV: Left-Right"))
# plot different panels by DV
plot.soc <- ggplot(filter(dat,model=="social"), 
         aes(x = religion, y = coef, color = religion, shape = religion)) +
  geom_hline(yintercept = 0, colour = gray(0.5),lty = 2) +
  geom_pointrange(aes(ymin = lwr95,ymax=upr95), lwd = 0.5, position = position_dodge(width = 0.5)) +
  facet_grid(. ~ trait, labeller = labeller_bf) +
  scale_y_continuous(breaks=c(-4,-2,0,2)) +
  scale_colour_discrete(name = "Sub-group Sample:",
                      labels=c("All", "Secular", "Traditional", "Religious",
                               "Very Religious\n/Ultra-Orthodox")) +
  scale_shape_discrete(name = "Sub-group Sample:",
                     labels=c("All", "Secular", "Traditional", "Religious",
                               "Very Religious\n/Ultra-Orthodox")) +
  labs(y = "Coefficient", x = "Subgroup Sample", title = "A. Social Policy") +
  theme_bw() +
  theme(
    panel.grid = element_blank(),
    axis.text.x = element_blank(),
    axis.title=element_text(size=10),
    plot.title=element_text(size=12),
    legend.position="bottom",
  )

plot.econ <- ggplot(filter(dat,model=="economic"), 
                   aes(x = religion, y = coef, color = religion, shape = religion)) +
  geom_hline(yintercept = 0, colour = gray(0.5),lty = 2) +
  geom_pointrange(aes(ymin = lwr95,ymax=upr95), lwd = 0.5, position = position_dodge(width = 0.5)) +
  facet_grid(. ~ trait, labeller = labeller_bf) +
  scale_y_continuous(breaks=c(-4,-2,0,2)) +
  scale_colour_discrete(name = "Sub-group Sample:",
                        labels=c("All", "Secular", "Traditional", "Religious",
                                 "Very Religious\n/Ultra-Orthodox")) +
  scale_shape_discrete(name = "Sub-group Sample:",
                       labels=c("All", "Secular", "Traditional", "Religious",
                                "Very Religious\n/Ultra-Orthodox")) +
  labs(y = "Coefficient", x = "Subgroup Sample", title = "B. Economic Policy") +
  theme_bw() +
  theme(
    panel.grid = element_blank(),
    axis.text.x = element_blank(),
    axis.title=element_text(size=10),
    plot.title=element_text(size=12),
    legend.position="bottom",
  )

plot.conf <- ggplot(filter(dat,model=="conflict"), 
                   aes(x = religion, y = coef, color = religion, shape = religion)) +
  geom_hline(yintercept = 0, colour = gray(0.5),lty = 2) +
  geom_pointrange(aes(ymin = lwr95,ymax=upr95), lwd = 0.5, position = position_dodge(width = 0.5)) +
  facet_grid(. ~ trait, labeller = labeller_bf) +
  scale_y_continuous(breaks=c(-4,-2,0,2)) +
  scale_colour_discrete(name = "Sub-group Sample:",
                        labels=c("All", "Secular", "Traditional", "Religious",
                                 "Very Religious\n/Ultra-Orthodox")) +
  scale_shape_discrete(name = "Sub-group Sample:",
                       labels=c("All", "Secular", "Traditional", "Religious",
                                "Very Religious\n/Ultra-Orthodox")) +
  labs(y = "Coefficient", x = "Subgroup Sample", title = "C. Conflict Policy") +
  theme_bw() +
  theme(
    panel.grid = element_blank(),
    axis.text.x = element_blank(),
    axis.title=element_text(size=10),
    plot.title=element_text(size=12),
    legend.position="bottom",
  )

plot.lr <- ggplot(filter(dat,model=="lr"), 
                   aes(x = religion, y = coef, color = religion, shape = religion)) +
  geom_hline(yintercept = 0, colour = gray(0.5),lty = 2) +
  geom_pointrange(aes(ymin = lwr95,ymax=upr95), lwd = 0.5, position = position_dodge(width = 0.5)) +
  facet_grid(. ~ trait, labeller = labeller_bf) +
  scale_y_continuous(breaks=c(-4,-2,0,2)) +
  scale_colour_discrete(name = "Sub-group Sample:",
                        labels=c("All", "Secular", "Traditional", "Religious",
                                 "Very Religious\n/Ultra-Orthodox")) +
  scale_shape_discrete(name = "Sub-group Sample:",
                       labels=c("All", "Secular", "Traditional", "Religious",
                                "Very Religious\n/Ultra-Orthodox")) +
  labs(y = "Coefficient", x = "Subgroup Sample", title = "D. Left-Right") +
  theme_bw() +
  theme(
    panel.grid = element_blank(),
    axis.text.x = element_blank(),
    axis.title=element_text(size=10),
    plot.title=element_text(size=12),
    legend.position="bottom",
  )
# combine panels to one graph
plot.fe2 <- plot.soc + plot.econ + plot.conf + plot.lr + 
  plot_layout(ncol =  1, guides="collect") & theme(legend.position = 'bottom')

# save file
ggsave("Figure E2.pdf", plot=plot.fe2,  width=8, height=8)